package com.liangzhong.model.goods.vo
{
	import com.common.utils.ObjectUtil;
	import com.global.GlobalDef;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.buffer.vo.BufferVo;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.Dictionary;
	
	import lz.controls.ImageContain;

	/**
	 *物品vo 
	 * @author lake
	 * 
	 */	
	public class GoodsVo
	{
		/**
		 *物品id 唯一性  -- 格子id
		 */		
		public var pid 			:int;
		/**
		 *物品表中的id 
		 */		
		public var gid			:int;
		/**
		 *物品名称 
		 */		
		public var name 		:String = "";
		/**
		 *物品类型 
		 */		
		public var type 		:int;
		/**
		 *物品子类型 
		 */		
		public var ntype		:int;
		/**
		 *物品头像地址 
		 */		
		public var url			:String;
		/**
		 *头像 相对地址 
		 */		
		public var header 		:String;
		/**
		 *大图片地址 
		 */		
		public var bigheader 	:String;
		/**
		 *欧元价格 
		 */		
		public var coin			:int;
		/**
		 *声望价格 
		 */		
		public var fame			:int;
		/**
		 *点券价格 
		 */		
		public var cashpoint	:int;
		/**
		 *贡献点价格 
		 */		
		public var devote		:int;
		/**
		 *对抗点价格 
		 */		
		public var rival		:int;
		/**
		 *荣耀点价格 
		 */		
		public var honour		:int;
		/**
		 *联赛点价格 
		 */		
		public var unions		:int;
		/**
		 *描述 
		 */		
		public var descplay		:String;
		/**
		 *buffid 
		 */		
		public var buffid		:int;
		/**
		 *品质
		 * 灰 绿 蓝 紫 橙 红 黑
		 * 1 2 3 4 5 6 7 
		 */		
		public var color		:int;
		/**
		 *是否加锁
		 * 1 -- 加锁
		 * 2 -- 不加锁 
		 */		
		public var locked		:int;
		/**
		 *位图数据 
		 */		
		private var _bitmapData	:BitmapData;
		/**
		 *大位图数据 
		 */		
		private var _bigBitmapData	:BitmapData;
		/**
		 *缓存位图数据 
		 * 避免同一个图片加载多次 
		 */		
		private static var bitmapDataDic :Dictionary = new Dictionary();
		
		public function GoodsVo()
		{
		}
		/**
		 *解析数据 
		 * @param data
		 * 
		 */		
		public function parse(data :Object):void
		{
			pid 	= data.pid;
		}
		/**
		 *深复制一个对象 
		 * @return 
		 * 
		 */		
		public function clone():GoodsVo
		{
			return ObjectUtil.deepCopy(this) as GoodsVo;
		}
		/**
		 *获取图像数据 
		 * @return 
		 * 
		 */		
		public function get bitmapData():BitmapData
		{
			if(bitmapDataDic[url] == null)
			{
				_bitmapData = new BitmapData(50,50,true,0x00000000);
				var imgContain 	:ImageContain = new ImageContain();
				imgContain.addEventListener(Event.COMPLETE,onComplete);
				imgContain.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
				imgContain.setSize(50,50);
				imgContain.url =  url;
				bitmapDataDic[url] = _bitmapData;
			}
			return bitmapDataDic[url];
		}
		/**
		 *获取大位图数据 
		 * @return 
		 * 
		 */		
		public function get bigBitmapData():BitmapData
		{
			if(bitmapDataDic[bigheader] == null)
			{
				_bigBitmapData = new BitmapData(152,168,true,0x00000000);
				var imgContain 	:ImageContain = new ImageContain();
				imgContain.addEventListener(Event.COMPLETE,onBigComplete);
				imgContain.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
				imgContain.setSize(150,168);
				imgContain.url =  bigheader;
				bitmapDataDic[bigheader] = _bigBitmapData;
			}
			return bitmapDataDic[bigheader];
		}
		/**
		 *io 错误 
		 * @param e
		 * 
		 */		
		private function onIoErrorHandler(e :IOErrorEvent):void
		{
			var imgContain 	:ImageContain = e.target as ImageContain;
			imgContain.removeEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
		}
		/**
		 *加载完毕 
		 * @param e
		 * 
		 */		
		private function onComplete(e :Event):void
		{
			var imgContain 	:ImageContain = e.target as ImageContain;
			imgContain.removeEventListener(Event.COMPLETE,onComplete);
			bitmapData.draw(imgContain);
			imgContain.clear();
			imgContain = null;
		}
		/**
		 *加载完毕 
		 * @param e
		 * 
		 */		
		private function onBigComplete(e :Event):void
		{
			var imgContain 	:ImageContain = e.target as ImageContain;
			imgContain.removeEventListener(Event.COMPLETE,onBigComplete);
			bigBitmapData.draw(imgContain);
			imgContain.clear();
			imgContain = null;
		}
		/**
		 *是否贵重物品 
		 * @return 
		 * 
		 */		
		public function get isPrecious():Boolean
		{
			return false;
		}
		/**
		 *获取tips 显示信息 
		 * @return 
		 * 
		 */		
		public function get tipsInfo():String
		{
			var colors :Array = ["#999999","#00FF66","#0033CC","#660099","#FFFF33","#FF0000","#cc0000","#FFFF00"];
			var colorDesc :Array = ["山寨","廉价","普通","精致","昂贵","顶级","逆天","神性"];
			var info :String = "<p align='left'><font color='"+ colors[color-1] +"'>" + name + "</font>\n\n";
			info += "品质:" + colorDesc[color-1];
			info += "\n</p>";
			var bufferVo :BufferVo = AppData.getInstance().bufferManager.getBufferById(buffid);
			if(bufferVo)
			{
				info += bufferVo.bufferTips;
			}
			return info;
		}
	}
}